﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class Serial_Number : Form
    {
        public Serial_Number()
        {
            InitializeComponent();
        }

        private void load_SPBAN()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select SANPHAM.IDsanpham,tensanpham,hanbaohanh from PHIEUXUAT_CT join SANPHAM on PHIEUXUAT_CT.IDsanpham = SANPHAM.IDsanpham where ngayxuat = @ngay";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@ngay", datenow.Value.ToShortDateString());

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table SANPHAMBAN");
            DataTable dt = ds.Tables["Table SANPHAMBAN"];

            DSSPDB.AutoGenerateColumns = false;
            DSSPDB.DataSource = dt.DefaultView;            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            load_SPBAN();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int i = DSSPDB.SelectedRows[0].Index;
            string hbh_sp = DSSPDB.Rows[i].Cells["hanbaohanh"].Value.ToString();
            DateTime datebh;

            if (hbh_sp == "12T")
                datebh = datenow.Value.AddYears(1);
            else if (hbh_sp == "24T")
                datebh = datenow.Value.AddYears(2);
            else
                datebh = datenow.Value.AddYears(3);

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "insert into SERINUMBER(serinumber,hanchotbaohanh) values (@seri,@hanchotbh)";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@seri", textBox1.Text);
            cmd.Parameters.AddWithValue("@hanchotbh", datebh);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}
